<?php
	include("library/vars.php");
	$getshow = $show;
	
	if(isset($_POST['submit'])) { // If the form is submited

		$get_episodes = $_POST["eps"];
		
		if ($get_episodes != null) {
				
			$get_episodes = str_replace('ep', '', $get_episodes);
			$how_many = count($get_episodes);
			if ($how_many>0) {
				for ($i=0; $i<$how_many; $i++) {
					$episode_nr = $get_episodes[$i];
					include("library/parse_fix.php");
					include("library/get_episode.php");
				
					$title = addslashes($title);
					$description = addslashes($summary);
				 
					include("library/config.php");
					include("library/opendb.php");
				 
					// Check to see if the episode already exists
					$query="SELECT * FROM episodes WHERE episode='$episode_nr' AND season_ID='$season' AND show_ID='$show'";
					$result = mysql_query($query);
					$num=mysql_numrows($result);
				 
					if ($num==0) { // If there's no episode with that number...
						$query2 = "INSERT INTO episodes (show_ID, season_ID, episode, title, description) VALUES ('$show', '$season', '$episode_nr', '$title', '$description')";
						mysql_query($query2) or die('Database Error');
				 
						include 'library/closedb.php';
					
					} 
					else { // If there's already an episode, let's override it...
						$query2 = "UPDATE episodes SET show_ID='$show', season_ID='$season', episode='$episode_nr', title='$title', description='$description' WHERE episode='$episode_nr' AND season_ID='$season' AND show_ID='$show'";
						mysql_query($query2) or die('Database Error');
						include 'library/closedb.php';	
					}
				}
				header( 'Location: index.php?s=' . $show . '&t=' . $season . '&m=episodes_imported' );
			}
		}
		else {
			header( 'Location: index.php?s=' . $show . '&t=' . $season . '' );
		}
	}


	else { // Show Episodes List and Form

	include("library/header.php");
	include("library/parse_fix.php");
	include("library/list_episodes.php");

echo '<div id="main">
		<div id="seasons">
			<h2>' . _("Import Episodes for") . ': <strong>' . $showname . '</strong> <small>('. _("Season") .' '. $season .')</small></h2>
		</div>';

	if ($number < 1) { 
		echo '<div id="messages">
			<p class="erro">' . _("There were no episodes found for this Show/Season") . '.</p>
		
			<p class="get_episode2"><a class="button" href="add_episode.php?s=' . $show . '&amp;t=' . $season . '">' . _("Add an episode manually") . '</a> ' . _("or") . ' <a href="index.php?s=' . $show . '&amp;t=' . $season . '">' . _("Cancel") . '</a>.</p>
		</div>
		';
	} else {
	echo'
	<h2>' . _("Select the episodes you wish to import") . ':</h2>
	<p class="select_all"><a href="#" id="checkboxall">' . _("Select All") . '</a></p>
	<div id="show">
		<p class="erro">' . _("<strong>CAUTION:</strong> The imported episodes will overwrite your existing data if you are importing episodes you already have!") . '</p>
		<p class="bytv">' . _("Data provided by") . ':<br /><img src="library/images/epguides.png" alt="epguides.com" class="img_logo" /> &amp; <img src="library/images/tv.gif" alt="TV.com" class="img_logo" /></p>
	</div>
	<div id="get_episode_list">
	
		<form method="post" id="import" action="">
			<ul>
			';
	
	$oddrow = 'odd';
	foreach($arrayShows as $value) {

 
    	$episodenumber = trim(substr($value['episode'],-2, 2), " ");
   		$value['title'] = str_replace(" &amp", "&", $value['title']);
		$value['title'] = str_replace("</a>", "", $value['title']);
		$title = $value['title'];
			
    	echo "<li class='" . $oddrow . "'><input type='checkbox' style='width:auto' name='eps[]' value='ep".$episodenumber."' id='ep".$episodenumber."' /><label for='ep".$episodenumber."'><span class='nr'>".$episodenumber."</span> ". $title."</label></li>
";

		if ('odd' == $oddrow) $oddrow = '';
		else $oddrow = 'odd';
	}
	
	echo '</ul>
	
	
	<p class="submit3"><input type="submit" name="submit" class="submit" id="submit" value="'. _("Import Selected Episodes").'" tabindex="4" /> '._("or").' <a href="index.php?s='.$show.'&amp;t='.$season.'">'._("Cancel").'</a></p>
	
	</form>
	</div>';
	}

?>

<div id="import_notice">
	<p><?php echo _("<strong>Importing episodes...</strong><br />This might take a while."); ?></p>
	<p class="nota"><?php echo _("Please don't close this window"); ?>.</p>
</div>

<div class="push"></div>

</div><!-- /#main -->
<?php include("library/footer.php");?>
</body>
</html>
<?php } ?>